package com.aracky.assess.service.impl;

import com.aracky.assess.mapper.HrKpiPostPerformanceConfigMapper;
import com.aracky.assess.model.entity.HrKpiPostPerformanceConfig;
import com.aracky.assess.model.request.HrKpiPostPerformanceConfigRequest;
import com.aracky.assess.model.request.PostDeptRequest;
import com.aracky.assess.model.vo.HrKpiPostPerformanceConfigTableVO;
import com.aracky.assess.model.vo.HrKpiPostPerformanceConfigVO;
import com.aracky.assess.model.vo.PerformanceAssessorTableVO;
import com.aracky.assess.model.vo.PostDeptVO;
import com.aracky.assess.service.HrKpiPostPerformanceConfigService;
import com.aracky.common.util.UserIPUtil;
import com.aracky.common.vo.PageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 岗位考核指标配置 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2022-08-01 09:43:18
 */
@Service
public class HrKpiPostPerformanceConfigServiceImpl extends ServiceImpl<HrKpiPostPerformanceConfigMapper, HrKpiPostPerformanceConfig> implements HrKpiPostPerformanceConfigService {
    @Resource
    private HrKpiPostPerformanceConfigMapper hrKpiPostPerformanceConfigMapper;

    @Resource
    private HrKpiPostPerformanceConfigService hrKpiPostPerformanceConfigService;


    /**
     * 列表查询
     * @param hrKpiPostPerformanceConfigRequest
     * @return
     */
    @Override
    public PageVO<HrKpiPostPerformanceConfigVO> getHrKpiPostPerformanceConfigList(HrKpiPostPerformanceConfigRequest hrKpiPostPerformanceConfigRequest) {
        IPage<HrKpiPostPerformanceConfigVO> page=hrKpiPostPerformanceConfigMapper.findHrKpiPostPermanceConfig(hrKpiPostPerformanceConfigRequest.toPage(),hrKpiPostPerformanceConfigRequest);
        return PageVO.convertPage(page);
    }

    /**
     * 分页查询出岗位考核指标的详情
     *
     * @param hrKpiPostPerformanceConfigRequest
     * @return
     */
    @Override
    public PageVO<HrKpiPostPerformanceConfigVO> getPageList(HrKpiPostPerformanceConfigRequest hrKpiPostPerformanceConfigRequest) {
        if (null == hrKpiPostPerformanceConfigRequest) {
            return null;
        }
        IPage<HrKpiPostPerformanceConfigVO> page = hrKpiPostPerformanceConfigMapper.selectPageList(hrKpiPostPerformanceConfigRequest.toPage(),hrKpiPostPerformanceConfigRequest);
        return PageVO.convertPage(page);
    }

    /**
     * 分页查询岗位部门列表信息
     *
     * @param postDeptRequest
     * @return
     */
    @Override
    public PageVO<PostDeptVO> getPostDept(PostDeptRequest postDeptRequest) {
        if (null == postDeptRequest) {
            return null;
        }
        // IPage<PostDeptVO> page = sysPostMapper.selectPostDept(postDeptRequest.toPage(),postDeptRequest);
        IPage<PostDeptVO> page = hrKpiPostPerformanceConfigMapper.selectPostDept(postDeptRequest.toPage(),postDeptRequest);
        return PageVO.convertPage(page);
    }

    /**
     * 查询详情表信息
     *
     * @param hrKpiPostPerformanceConfigRequest
     * @return
     */
    @Override
    public PerformanceAssessorTableVO getDetailTable(HrKpiPostPerformanceConfigRequest hrKpiPostPerformanceConfigRequest) {
        if (null == hrKpiPostPerformanceConfigRequest) {
            return null;
        }
        List<PerformanceAssessorTableVO> performanceAssessorTableVOList = hrKpiPostPerformanceConfigMapper.selectDetailTableHeader(hrKpiPostPerformanceConfigRequest.getPostId());
        if (performanceAssessorTableVOList.size() <= 0) {
            return null;
        }
        PerformanceAssessorTableVO performanceAssessorTableVO = performanceAssessorTableVOList.get(0);
        performanceAssessorTableVO.setHrKpiPostPerformanceConfigVOPageVo(hrKpiPostPerformanceConfigService.getHrKpiPostPerformanceConfigList(hrKpiPostPerformanceConfigRequest));
        return performanceAssessorTableVO;
    }

    /**
     * 根据postId查询岗位指标列表信息
     *
     * @param hrKpiPostPerformanceConfigRequest
     * @return
     */
    @Override
    public List<HrKpiPostPerformanceConfigVO> getList(HrKpiPostPerformanceConfigRequest hrKpiPostPerformanceConfigRequest) {
        List<HrKpiPostPerformanceConfigVO> hrKpiPostPerformanceConfigVOList = hrKpiPostPerformanceConfigMapper.selectByPostId(hrKpiPostPerformanceConfigRequest.getPostId());
        return hrKpiPostPerformanceConfigVOList;
    }

    /**
     * 逐条修改岗位指标列表
     *
     * @param hrKpiPostPerformanceConfigList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePostPerformanceConfigList(List<HrKpiPostPerformanceConfig> hrKpiPostPerformanceConfigList) throws Exception{
        int count = 0;
        for (HrKpiPostPerformanceConfig hrKpiPostPerformanceConfig : hrKpiPostPerformanceConfigList) {
            if (!UserIPUtil.getCurrentUserName().equals("") || null != UserIPUtil.getCurrentUserName()) {
                hrKpiPostPerformanceConfig.setUpdateBy(UserIPUtil.getCurrentUserName());
            }
            hrKpiPostPerformanceConfig.setUpdateTime(LocalDateTime.now());
            Integer update = hrKpiPostPerformanceConfigMapper.updatePostPerformanceConfig(hrKpiPostPerformanceConfig);
            count += update;
        }
        if (count < hrKpiPostPerformanceConfigList.size()){
            throw new Exception("修改失败！");
        }
    }

    /**
     * 根据postId查询岗位指标列表
     *
     * @param postId
     * @return
     */
    @Override
    public HrKpiPostPerformanceConfigTableVO getListByPostId(Long postId) {
        // 查询设置岗位指标列表信息
        HrKpiPostPerformanceConfigTableVO hrKpiPostPerformanceConfigTableVO = new HrKpiPostPerformanceConfigTableVO();
        List<HrKpiPostPerformanceConfigVO> hrKpiPostPerformanceConfigVOList = hrKpiPostPerformanceConfigMapper.selectByPostId(postId);
        HrKpiPostPerformanceConfigVO hrKpiPostPerformanceConfigVO = hrKpiPostPerformanceConfigVOList.get(0);
        hrKpiPostPerformanceConfigTableVO.setDeptName(hrKpiPostPerformanceConfigVO.getDeptName());
        hrKpiPostPerformanceConfigTableVO.setPostName(hrKpiPostPerformanceConfigVO.getPostName());
        hrKpiPostPerformanceConfigTableVO.setPostId(hrKpiPostPerformanceConfigVO.getPostId());
        hrKpiPostPerformanceConfigTableVO.setHrKpiPostPerformanceConfigVOList(hrKpiPostPerformanceConfigVOList);
        return hrKpiPostPerformanceConfigTableVO;
    }

}
